int PIN_PSI_ENG = 0;
int PIN_PSI_O2 = 1;
int PIN_PSI_FUEL = 2;
int PIN_PSI_N2 = 3;
int PIN_TEMP = 4;

int PIN_VALVE_VENT = 2;
int PIN_VALVE_HELIUM = 3;
int PIN_VALVE_ENGINE = 4;
int PIN_IGN = 5;

int STATUS_VALVE_VENT = 0;
int STATUS_VALVE_HELIUM = 0;
int STATUS_VALVE_ENGINE = 0;
int STATUS_IGN = 0;

float val[5];
float ratio[5];
float volts[5];
float pres[5];
int serRead = 0;

void setup()
{
  pinMode(PIN_VALVE_VENT, OUTPUT);
  pinMode(PIN_VALVE_HELIUM, OUTPUT);
  pinMode(PIN_VALVE_ENGINE, OUTPUT);
  pinMode(PIN_IGN, OUTPUT);
  
  digitalWrite(PIN_VALVE_VENT, STATUS_VALVE_VENT);
  digitalWrite(PIN_VALVE_HELIUM, STATUS_VALVE_HELIUM);
  digitalWrite(PIN_VALVE_ENGINE, STATUS_VALVE_ENGINE);
  digitalWrite(PIN_IGN, STATUS_IGN);
  
  Serial.begin(115200);
  Serial.println("UDART Rocket Controller");
  delay(1000);
}

void loop()
{

  val[0] = analogRead(0);
  ratio[0] = val[0] / 1024;
  volts[0] = ratio[0] * 5;
  pres[0] = ratio[0] * 1000 - 98;
  
  val[1] = analogRead(1);
  ratio[1] = val[1] / 1024;
  volts[1] = ratio[1] * 5;
  pres[1] = ratio[1] * 1000 - 92;
  
  val[2] = analogRead(2);
  ratio[2] = val[2] / 1024;
  volts[2] = ratio[2] * 5;
  pres[2] = ratio[2] * 1000 - 139;
  
  val[3] = analogRead(3);
  ratio[3] = val[3] / 1024;
  volts[3] = ratio[3] * 5;
  pres[3] = ratio[3] * 1000 - 136;
  
  if (Serial.available())
  { 
    serRead = Serial.read();
    
    if(serRead == 'S')
    {
      Serial.print((int)pres[0]);
      Serial.print(",");
      Serial.print((int)pres[1]);
      Serial.print(",");
      Serial.print((int)pres[2]);
      Serial.print(",");
      Serial.print((int)pres[3]);
      Serial.print(",");
      Serial.print((int)pres[4]);
      Serial.print(",");
      Serial.print(STATUS_VALVE_HELIUM);
      Serial.print(",");
      Serial.print(STATUS_VALVE_VENT);
      Serial.print(",");
      Serial.print(STATUS_VALVE_ENGINE);
      Serial.print(",");
      Serial.println(STATUS_IGN);
    }
    
    else if(serRead == 'v')
    {
      STATUS_VALVE_VENT = 0;
      digitalWrite(PIN_VALVE_VENT, STATUS_VALVE_VENT);
      Serial.println("ok");
    }
    
    else if(serRead == 'V')
    {
      STATUS_VALVE_VENT = 1;
      digitalWrite(PIN_VALVE_VENT, STATUS_VALVE_VENT);
      Serial.println("ok");
    }
    
    else if(serRead == 'h')
    {
      STATUS_VALVE_HELIUM = 0;
      digitalWrite(PIN_VALVE_HELIUM, STATUS_VALVE_HELIUM);
      Serial.println("ok");
    }
    
    else if(serRead == 'H')
    {
      STATUS_VALVE_HELIUM = 1;
      digitalWrite(PIN_VALVE_HELIUM, STATUS_VALVE_HELIUM);
      Serial.println("ok");
    }
    
    else if(serRead == 'e')
    {
      STATUS_VALVE_ENGINE = 0;
      digitalWrite(PIN_VALVE_ENGINE, STATUS_VALVE_ENGINE);
      Serial.println("ok");
    }
    
    else if(serRead == 'E')
    {
      STATUS_VALVE_ENGINE = 1;
      digitalWrite(PIN_VALVE_ENGINE, STATUS_VALVE_ENGINE);
      Serial.println("ok");
    }
    
    else if(serRead == 'i')
    {
      STATUS_IGN = 0;
      digitalWrite(PIN_IGN, STATUS_IGN);
      Serial.println("ok");
    }
    
    else if(serRead == 'I')
    {
      STATUS_IGN = 1;
      digitalWrite(PIN_IGN, STATUS_IGN);
      Serial.println("ok");
    }
  }
}  
